package com.china08.yxyapi.service.updateresource.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import javax.transaction.Transactional;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.china08.yxyapi.entity.db.res.YxyResBook;
import com.china08.yxyapi.entity.db.res.YxyResBookContents;
import com.china08.yxyapi.entity.db.res.YxyResPhase;
import com.china08.yxyapi.entity.db.res.YxyResResourceBookContent;
import com.china08.yxyapi.entity.db.res.YxyResResourceForEsReindex;
import com.china08.yxyapi.entity.db.res.YxyResSemester;
import com.china08.yxyapi.entity.db.res.YxyResSubject;
import com.china08.yxyapi.entity.db.res.YxyResVersion;
import com.china08.yxyapi.repository.db.YxyResBookContentsRepository;
import com.china08.yxyapi.repository.db.YxyResBookRepository;
import com.china08.yxyapi.repository.db.YxyResPhaseRepository;
import com.china08.yxyapi.repository.db.YxyResResourceBookContentRepository;
import com.china08.yxyapi.repository.db.YxyResResourceForEsReindexRepository;
import com.china08.yxyapi.repository.db.YxyResSemesterRepository;
import com.china08.yxyapi.repository.db.YxyResSubjectRepository;
import com.china08.yxyapi.repository.db.YxyResVersionRepository;
import com.china08.yxyapi.service.updateresource.ResourceUpdateService;

@Service
public class ResourceUpdateServiceImpl implements ResourceUpdateService {

    @Autowired
    private YxyResResourceBookContentRepository yxyResResourceBookContentRepository;

    @Autowired
    private YxyResBookContentsRepository YxyResBookContentsRepository;

    @Autowired
    private YxyResBookRepository yxyResBookRepository;

    @Autowired
    private YxyResPhaseRepository yxyResPhaseRepository;

    @Autowired
    private YxyResSubjectRepository yxyResSubjectRepository;

    @Autowired
    private YxyResSemesterRepository yxyResSemesterRepository;

    @Autowired
    private YxyResVersionRepository yxyResVersionRepository;

    @Autowired
    private YxyResResourceForEsReindexRepository yxyResResourceForEsReindexRepository;

    @Override
    public void updateResourceByBookcontentId(List<YxyResBookContents> bookContents) {
        for (YxyResBookContents bookContent : bookContents) {
            String bookcontentId = bookContent.getId();
            YxyResBookContents yxyResBookContents = YxyResBookContentsRepository.findById(bookcontentId);
            yxyResBookContents.setIsUpdate(0);
            YxyResBookContentsRepository.save(yxyResBookContents);
            List<YxyResResourceBookContent> yxyResResourceBookContents = yxyResResourceBookContentRepository
                    .findAllByBookContentPId(bookcontentId);
            if (yxyResResourceBookContents.size() == 0) {
                continue;
            }
            String bookId = yxyResBookContents.getBookId();
            YxyResBook book = yxyResBookRepository.findById(bookId);
            String phaseName = book.getPhaseName();
            String subjectName = book.getSubjectName();
            String versionName = book.getVersionName();
            String semesterName = book.getSemersterName();
            for (YxyResResourceBookContent yxyResResourceBookContent : yxyResResourceBookContents) {
                //
                String resBookContentId = yxyResResourceBookContent.getBookContentId();
                List<YxyResBookContents> parentsBookContents = new ArrayList<YxyResBookContents>();
                parentsBookContents = loopPids(resBookContentId, parentsBookContents);
                List<String> pids = parentsBookContents.stream().map(x -> x.getId()).collect(Collectors.toList());
                List<String> pName = parentsBookContents.stream().map(x -> x.getName()).collect(Collectors.toList());
                String bookcontentPid = StringUtils.join(pids, ",");
                String bookcontentPName = StringUtils.join(pName, ",");
                Collections.reverse(pName);
                String bookcontentName = StringUtils.join(pName, ",");
                // 名称： 所属 phase + subject + version + bookName(semesterName)
                // + bookcontentName
                String is = phaseName + "-" + subjectName + "-" + versionName + "-" + semesterName + "-"
                        + bookcontentName;
                yxyResResourceBookContent.setBookContentName(is);
                yxyResResourceBookContent.setBookContentPid(bookcontentPid);
                yxyResResourceBookContent.setBookContentPname(bookcontentPName);
                yxyResResourceBookContentRepository.save(yxyResResourceBookContent);
                YxyResResourceForEsReindex esReindex = new YxyResResourceForEsReindex();
                esReindex.setResourceId(yxyResResourceBookContent.getResourceId());
                yxyResResourceForEsReindexRepository.save(esReindex);
            }

        }
    }

    public List<YxyResBookContents> loopPids(String bookcontentId, List<YxyResBookContents> parent) {
        YxyResBookContents bookContents = YxyResBookContentsRepository.findById(bookcontentId);
        if (bookContents != null) {
            parent.add(bookContents);
            loopPids(bookContents.getpId(), parent);
        }
        return parent;
    }

    @Override
    @Transactional
    public void updateResourceBase(List<YxyResPhase> phases, List<YxyResSubject> subjects, List<YxyResVersion> versions,
            List<YxyResBook> books, List<YxyResSemester> semesters, List<YxyResBookContents> bookContents) {
        yxyResPhaseRepository.save(phases);
        yxyResSubjectRepository.save(subjects);
        yxyResVersionRepository.save(versions);
        yxyResBookRepository.save(books);
        yxyResSemesterRepository.save(semesters);
        YxyResBookContentsRepository.save(bookContents);
    }

}
